package com.demo.systemmonitor;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.demo.systemmonitor.R;

public class CpuMonitorActivity extends Activity {
    private static final String TAG = "CpuMonitorActivity";
    private static final int REQUEST_OVERLAY_PERMISSION = 100;
    
    private Button btnStartLargeWindow;
    private Button btnStartSmallWindow;
    private Button btnCloseWindow;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cpu_monitor_test);
        
        initViews();
        setupClickListeners();
        
        // 检查悬浮窗权限
        checkOverlayPermission();
    }
    
    private void initViews() {
        btnStartLargeWindow = findViewById(R.id.btn_start_large_window);
        btnStartSmallWindow = findViewById(R.id.btn_start_small_window);
        btnCloseWindow = findViewById(R.id.btn_close_window);
    }
    
    private void setupClickListeners() {
        btnStartLargeWindow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startFloatingWindow("largewindow");
            }
        });
        
        btnStartSmallWindow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startFloatingWindow("smallwindow");
            }
        });
        
        btnCloseWindow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startFloatingWindow("close");
            }
        });
    }
    
    private void checkOverlayPermission() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (!Settings.canDrawOverlays(this)) {
                Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                        Uri.parse("package:" + getPackageName()));
                startActivityForResult(intent, REQUEST_OVERLAY_PERMISSION);
            }
        }
    }
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_OVERLAY_PERMISSION) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                if (Settings.canDrawOverlays(this)) {
                    Toast.makeText(this, "悬浮窗权限已获取", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(this, "需要悬浮窗权限才能使用此功能", Toast.LENGTH_LONG).show();
                }
            }
        }
    }
    
    private void startFloatingWindow(String windowType) {
        try {
            // 通过FloatingWindowControlService控制悬浮窗
            Intent serviceIntent = new Intent(this, FloatingWindowControlService.class);
            serviceIntent.putExtra(FloatingWindowControlService.EXTRA_OPEN_FLOAT_WINDOW, windowType);
            startService(serviceIntent);
            
            // 启动数据收集服务（仅在大窗口时启动）
            if ("largewindow".equals(windowType)) {
                Intent dataServiceIntent = new Intent(this, SystemDataStorageService.class);
                startService(dataServiceIntent);
            }
            
            Log.d(TAG, "Floating window control service started with type: " + windowType);
            
            String message = "";
            switch (windowType) {
                case "largewindow":
                    message = "大悬浮窗已启动";
                    break;
                case "smallwindow":
                    message = "小悬浮窗已启动";
                    break;
                case "close":
                    message = "悬浮窗已关闭";
                    break;
            }
            Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
            
        } catch (Exception e) {
            Log.e(TAG, "Error controlling floating window", e);
            Toast.makeText(this, "操作失败: " + e.getMessage(), Toast.LENGTH_LONG).show();
        }
    }
}
